package com.sskj.aikeeper.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hjq.toast.ToastUtils;
import com.sskj.aikeeper.R;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.EditUtil;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;

/**
 * @author Hey
 * Create at  2019/06/25
 */
@Route(path = RConfig.APP_REGISTER_NUMBER)
public class RegisterActivity extends BaseActivity<RegisterPresenter> {

    @BindView(R.id.register_return_iv)
    ImageView registerReturnIv;
    @BindView(R.id.hintTv)
    TextView hintTv;
    @BindView(R.id.register_login_tv)
    TextView registerLoginTv;
    @BindView(R.id.register_number_et)
    EditText registerNumberEt;
    @BindView(R.id.register_code_et)
    EditText registerCodeEt;
    @BindView(R.id.register_getCode)
    TextView registerGetCode;
    @BindView(R.id.register_password_et)
    EditText registerPasswordEt;
    @BindView(R.id.register_password_iv)
    ImageView registerPasswordIv;
    @BindView(R.id.register_money_password_et)
    EditText registerMoneyPasswordEt;
    @BindView(R.id.register_money_iv)
    ImageView registerMoneyIv;
    @BindView(R.id.register_yaoqing_et)
    EditText registerYaoqingEt;
    @BindView(R.id.register_cb)
    CheckBox registerCb;
    @BindView(R.id.register_ok)
    TextView registerOk;
    @BindView(R.id.register_bt)
    Button registerBt;
    @BindView(R.id.tvXieyi)
    TextView tvXieyi;

    @Override
    public int getLayoutId() {
        return R.layout.app_activity_register;
    }

    @Override
    public RegisterPresenter getPresenter() {
        return new RegisterPresenter();
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        //协议
        ClickUtil.click(300, tvXieyi, () -> {
            ARouter.getInstance().build(RConfig.FUNCTIONS_REGISTER_AGREEMENT).navigation();
        });
        //点击注册
        ClickUtil.click(300, registerBt, () -> {
            if (isEmpty(registerNumberEt)) {
                ToastUtils.show(App.INSTANCE.getString(R.string.app_app_activity_register190));
                return;
            }
            if (!CheckUtil.isMobile(getText(registerNumberEt))) {
                ToastUtils.show(App.INSTANCE.getString(R.string.app_loginActivity1));
                return;
            }
            if (isEmpty(registerPasswordEt)) {
                ToastUtils.show(App.INSTANCE.getString(R.string.app_app_activity_change_login180));
                return;
            }
            if (!CheckUtil.isLoginPwd(getText(registerPasswordEt))) {
                ToastUtils.show(App.INSTANCE.getString(R.string.app_registerActivity2));
                return;
            }
            if (isEmpty(registerMoneyPasswordEt)) {
                ToastUtils.show(App.INSTANCE.getString(R.string.app_app_activity_register430));
                return;
            }
            if (!CheckUtil.isPayPwd(getText(registerMoneyPasswordEt))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.app_registerActivity3));
                return;
            }
            if (getText(registerPasswordEt).equals(getText(registerMoneyPasswordEt))) {
                ToastUtils.show(App.INSTANCE.getString(R.string.app_registerActivity4));
                return;
            }
            if (isEmpty(registerYaoqingEt)) {
                ToastUtils.show(App.INSTANCE.getString(R.string.app_app_activity_register520));
                return;
            }
            if (!registerCb.isChecked()) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.app_registerActivity5));
                return;
            }
            mPresenter.register(
                    registerYaoqingEt.getText().toString(),
                    registerNumberEt.getText().toString(),
                    registerCodeEt.getText().toString(),
                    registerPasswordEt.getText().toString(),
                    registerMoneyPasswordEt.getText().toString());
        });

        ClickUtil.click(300, registerReturnIv, this::finish);
        //密码显示
        ClickUtil.click(300, registerPasswordIv, () -> {
            EditUtil.togglePs(registerPasswordEt, registerPasswordIv);
        });

        ClickUtil.click(300, registerMoneyIv, () -> {
            EditUtil.togglePs(registerMoneyPasswordEt, registerMoneyIv);
        });
        //获取验证码
        ClickUtil.click(300, registerGetCode, () -> {
            mPresenter.getRegisterCode(registerNumberEt.getText().toString());
        });
        //获取验证码
        ClickUtil.click(300, registerLoginTv, this::finish);
    }


    public void startCount() {
        registerGetCode.setEnabled(false);
        disposableSubscriber = new DisposableSubscriber<Long>() {
            @Override
            public void onNext(Long aLong) {
                int time = (int) (60 - aLong);
                registerGetCode.setText(String.format(App.INSTANCE.getString(R.string.libIdCardVerification114), time));
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.toString());
            }

            @Override
            public void onComplete() {
                registerGetCode.setText(App.INSTANCE.getString(R.string.libIdCardVerification115));
                registerGetCode.setEnabled(true);
                if (!disposableSubscriber.isDisposed()) {
                    disposableSubscriber.dispose();
                    disposableSubscriber = null;
                }
            }
        };

        Flowable.interval(0, 1, TimeUnit.SECONDS, Schedulers.newThread())
                .take(60)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(disposableSubscriber);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, RegisterActivity.class);
        context.startActivity(intent);
    }


    public void registerSuccess() {
        finish();
    }

    public void registerCode() {
        startCount();
    }

}
